
# json中的dump
"""
dump() : 将Python对象转化为json ,并写入json文件。
"""

import json

data = {
  "name": "John",
  "age": 30,
  "city": "北京",
  "is_work": False,
  "card": None
}

# 1. 通过write进行写数据
# 将Python对象转化为json字符串
# z = json.dumps(data,ensure_ascii=False)   # ensure_ascii=False 代表保持原有编码
#
# with open('test_data1.json','w',encoding='utf-8') as f:
#     f.write(z)

# 2. 通过dump去写入json文件
with open('test_data2.json','w',encoding='utf-8') as f:
  json.dump(data,f,ensure_ascii=False)

